package testClasses.misc;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import gameAppLayer.enums.Constants;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLSandbox {
	
	
	public static void main(String[] args) throws DocumentException {
		XMLSandbox box = new XMLSandbox();
		
		box.start();
		
	}
	
	//Most code taken from:  http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html
	public void start() throws DocumentException {
		
		
	}
	
	public void readXMLDocument() throws DocumentException {
		//Read document
		SAXReader reader = new SAXReader();
		Document document = reader.read(Constants.assetPath + "//Data/TestXML.xml");		
		
		
		//--------------------------------------------------------
		//Iterate through document to retrieve data
		Element root = document.getRootElement();
		
		// iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element element = (Element) i.next();
            // do something
        }

        // iterate through child elements of root with element name "foo"
        for (Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {
            Element foo = (Element) i.next();
            // do something
        }

        // iterate through attributes of root 
        for (Iterator i = root.attributeIterator(); i.hasNext(); ) {
            Attribute attribute = (Attribute) i.next();
            // do something
        }
        //--------------------------------------------------------		
	}
	
	public Document createNewXMLDocument() {
		Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "root" );

        Element author1 = root.addElement( "author" )
            .addAttribute( "name", "James" )
            .addAttribute( "location", "UK" )
            .addText( "James Strachan" );
        
        Element author2 = root.addElement( "author" )
            .addAttribute( "name", "Bob" )
            .addAttribute( "location", "US" )
            .addText( "Bob McWhirter" );

        return document;
	}
	
	public void writeDataToXMLFile(Document document) throws IOException {
		 // lets write to a file
        XMLWriter writer = new XMLWriter(
            new FileWriter( "output.xml" )
        );
        writer.write( document );
        writer.close();


        // Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();
        writer = new XMLWriter( System.out, format );
        writer.write( document );

        // Compact format to System.out
        format = OutputFormat.createCompactFormat();
        writer = new XMLWriter( System.out, format );
        writer.write( document );
	}
	
	
	
	//Quick walk across XML document--useful for load/save activity?
	//--------------------------------------------------------
	public void treeWalk(Document document) {
		treeWalk( document.getRootElement() );
	}

	public void treeWalk(Element element) {
		for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
			Node node = element.node(i);
			if ( node instanceof Element ) {
				treeWalk( (Element) node );
			}
			else {
				// do something....
			}
		}
	}

	//--------------------------------------------------------
        
        
		
		
		
		
		
	

}
